using System;
using System.Collections.Generic;
using System.Text;


using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace buildingLib.camera
{
    [Serializable()]
    public class Camera : BusinessBase<Camera>
    {
        #region Business Methods
        Guid m_id;
        string m_name = string.Empty;
        string m_modelname = string.Empty;
        double m_posX=0;
        double m_posY=0;
        double m_posZ=0;
        double m_dirX=0;
        double m_dirY=0;
        double m_dirZ=0;
        bool m_hasPos = false;

        public bool HasPosition
        {
            get { return m_hasPos; }
        }

        public Guid ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }

        public string Name
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_name;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_name != value)
                {
                    m_name = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Modelname
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_modelname;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_modelname != value)
                {
                    m_modelname = value;
                    PropertyHasChanged();
                }
            }
        }
        public double PosX
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_posX;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_posX != value)
                {
                    m_posX = value;
                    PropertyHasChanged();
                }
            }
        }
        public double PosY
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_posY;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_posY != value)
                {
                    m_posY = value;
                    PropertyHasChanged();
                }
            }
        }
        public double PosZ
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_posZ;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_posZ != value)
                {
                    m_posZ = value;
                    PropertyHasChanged();
                }
            }
        }
        public double DirX
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_dirX;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_dirX != value)
                {
                    m_dirX = value;
                    PropertyHasChanged();
                }
            }
        }
        public double DirY
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_dirY;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_dirY != value)
                {
                    m_dirY = value;
                    PropertyHasChanged();
                }
            }
        }
        public double DirZ
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_dirZ;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_dirZ != value)
                {
                    m_dirZ = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return m_id;
        }
        #endregion



        #region Factory Methods

        public  static Camera NewCamera(Guid doorID)
        {
            // TODO: change to use new keyword if not loading defaults
            return new Camera(doorID);
        }

        public  static Camera GetCamera(Guid doorID)
        {
            return DataPortal.Fetch<Camera>(new Criteria(doorID));
        }
        public static Camera GetCamera(string modelName)
        {
            return DataPortal.Fetch<Camera>(new MCriteria(modelName));
        }

        public static void Delete(Camera camera)
        {
            DataPortal.Delete(new Criteria(camera.ID));
        }

        private Camera(Guid doorID)
        {
            m_id = doorID;
        }
        private Camera() { }

        public override Camera Save()
        {
            return base.Save();
        }
        #endregion

        #region Data Access
        [Serializable()]
        private class Criteria
        {
            private Guid _id;
            public Guid Id
            {
                get { return _id; }
            }
            public Criteria() { }
            public Criteria(Guid doorid)
            { _id = doorid; }
        }

        [Serializable()]
        private class MCriteria : Criteria
        {
            string _modelName;
            public string ModelName
            {
                get { return _modelName; }
            }
            
            public MCriteria(string modelName)
            {
                _modelName = modelName;
            }
        }

        [RunLocal()]
        protected override void DataPortal_Create()
        {
            
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GGetCameraByDoorID");
            db.AddInParameter(dbcom, "id", DbType.Guid, criteria.Id);            
            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                if (dr.Read())
                {
                    Fetch(dr);
                }                                              
            }
        }

        private void DataPortal_Fetch(MCriteria criteria)
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GGetCameraByModelName");
            db.AddInParameter(dbcom, "name", DbType.String, criteria.ModelName);
            
            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                if (dr.Read())
                {
                    Fetch(dr);
                }                
            }
        }

        private void Fetch(SafeDataReader dr)
        {
            m_id = dr.GetGuid("doorid");
            m_name = dr.GetString("name");
            m_modelname = dr.GetString("modelname");
            m_posX = dr.GetDouble("posX");
            m_posY = dr.GetDouble("posY");
            m_posZ = dr.GetDouble("posZ");
            m_dirX = dr.GetDouble("dirX");
            m_dirY = dr.GetDouble("dirY");
            m_dirZ = dr.GetDouble("dirZ");
            m_hasPos = true;                    
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GInsertDoorCamera");
            DoInsertUpdate(dbcom);           
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            if (base.IsDirty)
            {
                Database db = SQLDatabase.GetDatabase();
                DbCommand dbcom = db.GetStoredProcCommand("usp_GUpdateDoorCamera");
                DoInsertUpdate(dbcom);            
            }           
        }

       

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(m_id));
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Criteria criteria)
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GDeleteDoorCamera");
            db.AddInParameter(dbcom, "doorid", DbType.Guid, m_id);

            db.ExecuteNonQuery(dbcom);
        }
     

      

        private void DoInsertUpdate(DbCommand dbcom)
        {
            Database db = SQLDatabase.GetDatabase();
            db.AddInParameter(dbcom, "doorid", DbType.Guid, m_id);
            db.AddInParameter(dbcom, "name", DbType.String, m_name);
            db.AddInParameter(dbcom, "modelname", DbType.String, m_modelname);
            db.AddInParameter(dbcom, "posX", DbType.Single, m_posX);
            db.AddInParameter(dbcom, "posY", DbType.Single, m_posY);
            db.AddInParameter(dbcom, "posZ", DbType.Single, m_posZ);
            db.AddInParameter(dbcom, "dirX", DbType.Single, m_dirX);
            db.AddInParameter(dbcom, "dirY", DbType.Single, m_dirY);
            db.AddInParameter(dbcom, "dirZ", DbType.Single, m_dirZ);

            db.ExecuteNonQuery(dbcom);
        }

        
        #endregion
    }
}
